package com.promis.party.ui.userinfo;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.promis.compat.StatusBarCompat;
import com.promis.crypto.digest.MD5Digest;
import com.promis.crypto.symmetric.PBECodeCipher;
import com.promis.okhttp.HttpJsonParams;
import com.promis.party.R;
import com.promis.party.helper.ContextHelper;
import com.promis.party.helper.UIHepler;
import com.promis.party.preference.AccountPreferences;
import com.promis.party.preference.UserPreferences;
import com.promis.party.ui.MainActivity;

import javax.crypto.SecretKey;

public class UpdatePasswordActivity extends AppCompatActivity {

    private ContextHelper contextHelper;

    private EditText edtvOldPassword;
    private EditText edtvNewPassword;
    private EditText edtvConfirmPassword;
    private TextView txtvSubmit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        StatusBarCompat.translucentStatusBar(this);
        StatusBarCompat.blackTextStatusBar(this);
        setContentView(R.layout.activity_update_password);
        UIHepler.goBack(UpdatePasswordActivity.this, R.id.activity_update_password_imgv_back);

        edtvOldPassword = (EditText) findViewById(R.id.activity_update_password_txtv_oldpasswd);
        edtvNewPassword = (EditText) findViewById(R.id.activity_update_password_txtv_newpasswd);
        edtvConfirmPassword = (EditText) findViewById(R.id.activity_update_password_txtv_confirmpasswd);
        txtvSubmit = (TextView) findViewById(R.id.activity_update_password_txtv_submit);

        initData();
        initListener();
    }

    private void initData() {
        contextHelper = new ContextHelper(getBaseContext());
    }

    private void initListener() {

        txtvSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    updatePassword();
                } catch (Exception e) {
                    Toast.makeText(getBaseContext(), "密钥加密失败", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private void updatePassword() throws Exception {
//        String account = edtvAccount.getText().toString();
//        String password = edtvPassword.getText().toString();
//
//        HttpJsonParams params = new HttpJsonParams();
//        params.add("account", account);
//
//        MD5Digest md5 = MD5Digest.getInstance();
//        PBECodeCipher cipher = PBECodeCipher.getInstance();
//        byte[] md5Key = md5.digest(password.getBytes());
//
//        byte[] salt = cipher.initSalt();
//        SecretKey secretKey = cipher.toKey(account);
//
//        byte[] data = cipher.encrypt(md5Key, secretKey, salt);
//
//        String pwd = cipher.encryptBASE64(data);
//        String ransalt = cipher.encryptBASE64(salt);
//        params.add("password", pwd);
//        params.add("ransalt", ransalt);
////        params.add("cid", PushManager.getInstance().getClientid(contextHelper.getContext()));
//
//        UserPreferences.getInstance(getBaseContext()).remove();
//        AccountPreferences.getInstance(getBaseContext()).remove();
//        String url = contextHelper.getServiceUrl(R.string.login);
////        new HttpClient<JsonLogin>(getBaseContext()){
////
////            @Override
////            protected void onSuccess(JsonLogin result) {
////                User user = result.getUser();
////
////                if (user != null) {
////                    UserPreferences.getInstance(getBaseContext()).put(user);
////                    AccountPreferences.getInstance(getBaseContext()).put(user.getAccount(), user.getPassword(), user.getState());
////                }
////
////                Toast.makeText(getBaseContext(), result.getMsgContent(), Toast.LENGTH_SHORT).show();
////                Intent intent = new Intent(getContext(), MainActivity.class);
////                startActivity(intent);
////                LoginActivity.this.finish();
////            }
////
////            @Override
////            protected void onError(String errorMsg) {
////                Toast.makeText(getBaseContext(), errorMsg, Toast.LENGTH_SHORT).show();
////            }
////        }.postAsyn(url, params);
    }
}
